<template>
  <div class="learning-paths-container">
    <h1>我的学习路径</h1>
    
    <div class="learning-paths-header">
      <p>选择一个学习路径开始系统化学习</p>
      <div class="filters">
        <select v-model="statusFilter" class="filter-select">
          <option value="all">所有状态</option>
          <option value="not_started">未开始</option>
          <option value="in_progress">学习中</option>
          <option value="completed">已完成</option>
        </select>
      </div>
    </div>
    
    <div v-if="loading" class="loading">
      <div class="spinner"></div>
      <p>加载学习路径中...</p>
    </div>
    
    <div v-else-if="error" class="error-message">
      <p>{{ error }}</p>
      <button @click="fetchLearningPaths" class="btn-retry">重试</button>
    </div>
    
    <div v-else-if="filteredLearningPaths.length === 0" class="empty-state">
      <div class="empty-icon">📚</div>
      <h3>暂无学习路径</h3>
      <p>请先上传文档并生成知识卡片，然后创建学习路径</p>
      <router-link to="/documents" class="btn-primary">浏览文档</router-link>
    </div>
    
    <div v-else class="learning-paths-grid">
      <div 
        v-for="path in filteredLearningPaths" 
        :key="path._id" 
        class="learning-path-card"
        @click="viewLearningPath(path._id)"
      >
        <div class="card-header">
          <span :class="['status-badge', getStatusClass(path.status)]">
            {{ getStatusText(path.status) }}
          </span>
          <span class="document-name" :title="path.documentTitle">
            {{ path.documentTitle }}
          </span>
        </div>
        
        <h3 class="path-title">{{ path.title }}</h3>
        <p class="path-description">{{ path.description }}</p>
        
        <div class="progress-container">
          <div class="progress-bar">
            <div class="progress-fill" :style="{ width: `${path.progress}%` }"></div>
          </div>
          <span class="progress-text">{{ path.progress }}% 完成</span>
        </div>
        
        <div class="card-footer">
          <button class="btn-continue">
            {{ path.status === 'not_started' ? '开始学习' : 
               path.status === 'completed' ? '复习' : '继续学习' }}
          </button>
          <span class="last-accessed" v-if="path.lastAccessedAt">
            上次学习: {{ formatDate(path.lastAccessedAt) }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { learningPathApi } from '@/services/api';

const router = useRouter();
const learningPaths = ref([]);
const loading = ref(true);
const error = ref(null);
const statusFilter = ref('all');

// 过滤学习路径
const filteredLearningPaths = computed(() => {
  if (statusFilter.value === 'all') {
    return learningPaths.value;
  }
  return learningPaths.value.filter(path => path.status === statusFilter.value);
});

// 获取所有学习路径
const fetchLearningPaths = async () => {
  loading.value = true;
  error.value = null;
  
  try {
    console.log('[LearningPathsView] 开始获取学习路径');
    const response = await learningPathApi.getUserPaths();
    console.log('[LearningPathsView] 获取学习路径响应:', response.data);
    
    if (response.data && response.data.learningPaths) {
      learningPaths.value = response.data.learningPaths;
      console.log(`[LearningPathsView] 成功获取 ${learningPaths.value.length} 条学习路径`);
    } else {
      console.warn('[LearningPathsView] 响应中没有 learningPaths 数据');
      learningPaths.value = [];
    }
  } catch (err) {
    console.error('[LearningPathsView] 获取学习路径失败:', err);
    error.value = '获取学习路径失败，请稍后重试';
  } finally {
    loading.value = false;
  }
};

// 查看学习路径详情
const viewLearningPath = (id) => {
  router.push(`/learning-paths/${id}`);
};

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString);
  return date.toLocaleDateString('zh-CN', { 
    year: 'numeric', 
    month: 'short', 
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit'
  });
};

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    'not_started': '未开始',
    'in_progress': '学习中',
    'completed': '已完成'
  };
  return statusMap[status] || status;
};

// 获取状态类名
const getStatusClass = (status) => {
  return status.replace('_', '-');
};

onMounted(() => {
  fetchLearningPaths();
});
</script>

<style scoped>
.learning-paths-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

h1 {
  margin-bottom: 1.5rem;
  color: #333;
}

.learning-paths-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
}

.learning-paths-header p {
  color: #666;
  font-size: 1.1rem;
}

.filters {
  display: flex;
  gap: 1rem;
}

.filter-select {
  padding: 0.5rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  background-color: white;
  font-size: 0.9rem;
}

.learning-paths-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 2rem;
}

.learning-path-card {
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 1.5rem;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  cursor: pointer;
}

.learning-path-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.status-badge {
  padding: 0.3rem 0.6rem;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: bold;
}

.not-started {
  background-color: #e3f2fd;
  color: #1976d2;
}

.in-progress {
  background-color: #fff8e1;
  color: #ff8f00;
}

.completed {
  background-color: #e8f5e9;
  color: #2e7d32;
}

.document-name {
  font-size: 0.9rem;
  color: #666;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.path-title {
  margin-bottom: 0.5rem;
  font-size: 1.2rem;
}

.path-description {
  color: #666;
  margin-bottom: 1.5rem;
  font-size: 0.9rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.progress-container {
  margin-bottom: 1.5rem;
}

.progress-bar {
  height: 8px;
  background-color: #f1f1f1;
  border-radius: 4px;
  margin-bottom: 0.5rem;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background-color: #4CAF50;
  border-radius: 4px;
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 0.9rem;
  color: #666;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.btn-continue {
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 0.6rem 1rem;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.2s ease;
}

.btn-continue:hover {
  background-color: #45a049;
}

.last-accessed {
  font-size: 0.8rem;
  color: #999;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 3rem;
}

.spinner {
  border: 4px solid rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  border-top: 4px solid #4CAF50;
  width: 40px;
  height: 40px;
  animation: spin 1s linear infinite;
  margin-bottom: 1rem;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.error-message {
  text-align: center;
  padding: 3rem;
  color: #d32f2f;
}

.btn-retry {
  background-color: #f1f1f1;
  border: none;
  border-radius: 4px;
  padding: 0.6rem 1rem;
  margin-top: 1rem;
  cursor: pointer;
}

.empty-state {
  text-align: center;
  padding: 3rem;
}

.empty-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.btn-primary {
  display: inline-block;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 0.6rem 1rem;
  margin-top: 1rem;
  text-decoration: none;
  font-weight: bold;
}

@media (max-width: 768px) {
  .learning-paths-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
  }
  
  .learning-paths-grid {
    grid-template-columns: 1fr;
  }
}
</style> 